import { Form, Space, Tag, DatePicker, Radio, InputNumber, Select, Tooltip, Icon, Switch, Input } from "@bytedance/mona-ui";
import React, { useEffect, useState, forwardRef, useImperativeHandle } from "react";
import { selectAcTimeList } from './ltalpFunc'
import moment from 'moment';
// import { createTaskId } from "../../../jinritemaiCopyGoods/copyDataFomate"

const { RangePicker } = DatePicker;
const { Option } = Select;
let old
let setOll
function CopyActBoxCoentent(props: any, ref: any) {
    const { copyData, autoSetConfigData } = props;
    if (!copyData) return null;
    console.log('copyData', copyData)
    function disabledDate(current) {
        return current && current < moment().subtract(1, 'days') || current > moment().add(7, 'days');
    }
    function disabledDateQter(current) {
        return current && current < moment().subtract(1, 'days') || current > moment().add(365, 'days');
    }
    function setCoyDataInfo() {
        const { begin_time, end_time } = copyData
        const endater = moment.duration(moment(end_time).unix() - moment(begin_time).unix(), 'seconds')
        const d = endater.days()
        const h = endater.hours()
        const m = endater.minutes()
        const s = endater.seconds()
        // console.log('d', d, 'h', h, 'm', m, 's', s)
        let selectGetTimeData = (new Date(end_time).getTime() - new Date(begin_time).getTime()) / 60 / 1000
        // console.log('selectGetTimeDataT', copyData)
        // copyData.time_set_type = copyData?.time_set_type * 1

        const updatas = {
            ...copyData,
            activtiTime: copyData.begin_time,
            selectTitlDater: (() => {
                if (h > 0) {
                    return 0
                }
                return 1
            })(),
            endTimeTmetr: m * 1 || 59,
            endTimeSmit: (d * 24 + h * 1) || 48,
            activetiQuantity: [begin_time, end_time],
            activetTimeBuy: [begin_time, end_time],
            selectGetTimeData
        }
        return updatas
    }
    const [addLtaLpConfig, setAddLtaLpConfig] = useState<any>({})
    const {
        title,
        business_code,
        activtiTime,
        selectTitlDater,
        endTimeTmetr,
        endTimeSmit,
        activetiQuantity,
        time_set_type,
        activetTimeBuy,
        selectGetTimeData
    } = addLtaLpConfig

    const [autoSetConfig, setAutoSetConfig] = useState<any>({
        autoNumber: 10, // 自动设置数量
        actTimeType: 1, // 1 活动相同时长 2 活动时长自定义
        deleteOldActStatus: true, // 删除旧活动
        notIsPre: 1, // 是否适配价格 1.适配最低价 2。跳过不符合项
        timeTypRedou: 1, // 按小时 按分钟
        minutesLen: 59, // 分钟长度
        hoursLen: 48, // 小时长度
        befoTitle: '', // 前缀
        useNumber: 0, // 已经创建的次数
        cretedLog: [], // 创建记录 {title, id}
        stockSetType: 1, // 1同上次活动 2全部库存 3百分比
        stockPercentage: 20
    })
    if (autoSetConfigData) {

    }
    const {
        actTimeType,
        autoNumber,
        deleteOldActStatus,
        notIsPre,
        timeTypRedou,
        minutesLen,
        hoursLen,
        befoTitle,
        stockSetType,
        stockPercentage
    } = autoSetConfig

    // function putContentData() {
    //     window.postMessage({
    //         type: 'dly_activity_modole',
    //         dly_method: 'add',
    //         data: {
    //             addLtaLpConfig,
    //             autoSetConfig,
    //             id: createTaskId()
    //         }
    //     })
    // }

    useImperativeHandle(ref, () => ({
        getData: {
            addLtaLpConfig: (() => {
                addLtaLpConfig.promotion_goods = addLtaLpConfig?.promotion_goods?.map(item => {
                    return {
                        ...item,
                        autoNumber: autoSetConfig.autoNumber,
                        useNumber: item.useNumber || 0
                    }
                })
                return addLtaLpConfig
            })(),
            autoSetConfig
        }
    }))
    useEffect(() => {
        if (autoSetConfigData && autoSetConfigData !== setOll) {
            setOll = autoSetConfigData
            setAutoSetConfig(autoSetConfigData)
        }
    }, [autoSetConfigData])
    useEffect(() => {
        if (copyData !== old) {
            old = copyData
            setAddLtaLpConfig(setCoyDataInfo())
        }
    }, [copyData])
    // console.log('addLtaLpConfig', addLtaLpConfig)

    function gethourslen() {
        if (business_code === 'LimitTime' || business_code === '') {
            return 96
        }
        if (business_code === 'LimitQuantity') {
            return 24 * 30
        }

        return 24 * 365
    }

    function setextratext() {
        if (actTimeType === 1 || timeTypRedou === 2) {
            return null
        }
        if (business_code === 'LimitTime' || business_code === '') {
            return <span>限时活动可设置最长时长为 96 小时</span>
        }
        if (business_code === 'LimitQuantity') {
            return <span>限量活动可设置时长为 30 天（{30 * 24}小时）</span>
        }
        if (business_code === 'OrdinaryTimeBuy') {
            return <span>普通降价促销活动可设置时长为 365 天（{365 * 24}小时）</span>
        }
    }

    return (<Space direction="vertical">
        <Space direction="vertical" size={2}>
            <div className="tit">
                {title}
            </div>
            <Tag bordered color={
                (business_code === 'LimitTime' || business_code === '') ? '#6C26FD' : business_code === 'LimitQuantity' ? '#2B71FF' : '#252931'
            } transparentBg>{(business_code === 'LimitTime' || business_code === '') ? '限时抢购' : business_code === 'LimitQuantity' ? '限量抢购' : business_code === 'OrdinaryTimeBuy' ? '普通降价促销' : ''}</Tag>
        </Space>
        <Form>
            <Form.Item
                label={<Space>
                    <span>商品自动续期数</span>
                    <Tooltip title="任务创建后希望当前活动包含的商品自动创建的次数">
                        <Icon type="DoubtIcon"></Icon>
                    </Tooltip>
                </Space>}
            >
                <InputNumber min={1} step={1} unit="次" value={autoNumber} onChange={(e) => {
                    setAutoSetConfig({ ...autoSetConfig, autoNumber: e })
                }}></InputNumber>
            </Form.Item>
            <Form.Item
                label={<Space>
                    <span>活动时长</span>
                    <Tooltip title="创建活动开始的时间 到结束时间的时长">
                        <Icon type="DoubtIcon"></Icon>
                    </Tooltip>
                </Space>}
                extra={setextratext()}
            >
                <Space direction="vertical">
                    <Radio.Group size="small" value={actTimeType} onChange={(e) => {
                        setAutoSetConfig({ ...autoSetConfig, actTimeType: e.target.value })
                    }}>
                        <Radio value={1}>与原活动相同时长</Radio>
                        <Radio value={2}>自定义</Radio>
                    </Radio.Group>
                    {autoSetConfig.actTimeType === 2 && <>
                        <Radio.Group size="small" value={timeTypRedou} onChange={(e) => {
                            setAutoSetConfig({ ...autoSetConfig, timeTypRedou: e.target.value })
                        }}>
                            <Radio value={1}>按小时持续</Radio>
                            <Radio value={2}>按分钟持续</Radio>
                        </Radio.Group>
                        <InputNumber min={1} max={timeTypRedou === 1 ? gethourslen() : 59} unit={timeTypRedou === 1 ? '时' : '分'} value={timeTypRedou === 1 ? hoursLen : minutesLen} onChange={(e) => {
                            timeTypRedou === 1 ? setAutoSetConfig({ ...autoSetConfig, hoursLen: e }) : setAutoSetConfig({ ...autoSetConfig, minutesLen: e })
                        }}></InputNumber>
                    </>}

                </Space>
            </Form.Item>

            {/* <Form.Item
                label="删除原过期活动"
            >
                <Switch size="small" checked={deleteOldActStatus} onChange={(e) => {
                    setAutoSetConfig({ ...autoSetConfig, deleteOldActStatus: e })
                }}></Switch>
            </Form.Item> */}
            <Form.Item
                label={<Space>
                    <span>优惠力度不匹配</span>
                    <Tooltip title="选择设置活动是 当前活动的优惠力度不匹配时（将优惠力度调整至符合活动要求的最低优惠力度） 是否自动调整或者跳过当前商品">
                        <Icon type="DoubtIcon"></Icon>
                    </Tooltip>
                </Space>}
            >
                <Radio.Group value={notIsPre} onChange={(e) => {
                    setAutoSetConfig({ ...autoSetConfig, notIsPre: e.target.value })
                }}>
                    <Radio value={1}>适配最低的优惠力度</Radio>
                    <Radio value={2}>跳过不符合商品</Radio>
                </Radio.Group>
            </Form.Item>
            <Form.Item
                label="库存使用方案"
            >
                <Space direction="vertical">
                    <Radio.Group value={stockSetType} onChange={(e) => {
                        setAutoSetConfig({ ...autoSetConfig, stockSetType: e.target.value })
                    }}>
                        <Radio value={1}>使用当前活动相同库存
                            <Tooltip title="商品原库存不足时将使用全部库存，当存在sku库存为0时活动不可创建，将会自动跳过不可设置商品">
                                <Icon type="DoubtIcon"></Icon>
                            </Tooltip>
                        </Radio>
                        <Radio value={2}>使用全部库存</Radio>
                        <Radio value={3}>使用百分比</Radio>
                    </Radio.Group>
                    {stockSetType === 3 &&
                        <Input style={{
                            width: 200
                        }} value={stockPercentage} addonAfter="%" onChange={(e) => {
                            setAutoSetConfig({ ...autoSetConfig, stockPercentage: e.target.value })
                        }} maxLength={5} placeholder="最多支持5个"></Input>}
                </Space>

            </Form.Item>
            <Form.Item
                label="活动名称前缀"
            >
                <Input value={befoTitle} onChange={(e) => {
                    setAutoSetConfig({ ...autoSetConfig, befoTitle: e.target.value })
                }} maxLength={5} placeholder="最多支持5个"></Input>
            </Form.Item>
        </Form>
    </Space>)
}

export default forwardRef(CopyActBoxCoentent);